// pages/hospital-detail/hospital-detail.js
import { hospitalDetails, sucessCaseList, doctorList, featureService } from "../../api/apis.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospital: {},
    prductListWrap: false,
    productList: [],
    doctorList: [],
    exampleList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      mid: options.mid
    })
    this._initData()
  },
  _initData() {
    hospitalDetails({
      data: {HosID: this.data.mid},
      success: res => {
        if (res.state) {
          res.data.tag = res.data.Tags.split('|')
          this.setData({
            hospital: res.data
          })
        }
      }
    })
    this._getSuccess()
    this._getDoctor()
    this._getFeatureService()
  },
  _getSuccess() {
    sucessCaseList({
      data: {
        index: 1,
        size: 50,
        types: 1,
        mid: this.data.mid
      },
      success: res => {
        if (res.state) {
          let list = []
          res.data.list.forEach((item) => {
            list.push({
              mid: item.mid,
              beforeImg: item.skulist[0].img,
              afterImg: item.skulist[item.skulist.length - 1].img,
              name: item.PatientName,
              doctor: item.DocName,
              bSaying: item.skulist[0].Describe,
              aSaying: item.skulist[item.skulist.length - 1].Describe,
              surgery: item.Operation.split('，')
            })
          })
          this.setData({
            exampleList: list
          })
        }
      }
    })
  },
  _getDoctor() {
    doctorList({
      data: {
        isTop: false,
        HosID: this.data.mid,
        index: 1,
        size: 1
      },
      success: res => {
        if (res.state) {
          res.data.map((item) => {
            item.tag = item.Tags.split('|')
            return item
          })
          this.setData({
            doctorList: this.data.doctorList.concat(res.data)
          })
        }
      }
    })
  },
  _getFeatureService() {
    featureService({
      data: {
        hospital: this.data.mid,
        index: 1,
        size: 5
      },
      success: res => {
        if (res.state) {
          let productList = []
          res.data.list.forEach((item) => {
            productList.push({
              img: item.image.length > 0 ? item.image[0] : "",
              name: item.title,
              supply: item.yiyuan,
              sku: [{
                price: item.price,
                oriPrice: item.oriPrice
              }],
              mid: item.IptId
            })
          })
          this.setData({
            productList: productList
          })
        }
      }
    })
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.hospital.HosPhone
    })
  },
  getMap() {
    wx.openLocation({
      latitude: this.data.hospital.Positionlat,
      longitude: this.data.hospital.Positionlng,
      scale: 28
    })
  },
  gotoDoctorDetail(e) {
    wx.navigateTo({
      url: `../doctor-detail/doctor-detail?mid=${e.detail.item.mid}`,
    })
  },
  gotoExampleDetail(e) {
    wx.navigateTo({
      url: `../example-detail/example-detail?did=${e.detail.item.mid}`,
    })
  },
  gotoProjectDetail(e) {
    console.log(e)
    wx.navigateTo({
      url: `../project-detail/project-detail?did=${e.detail.item.mid}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})